/**
 * Copyright (C) 2013 AirChina
 *
 * @className:com.airchina.wechat.frontend.processor.main.Action
 * @description:
 * 
 * @version:v1.0.0 
 * @author:larry
 * 
 * Modification History:
 * NO   Date         Author      Version     Description
 * -----------------------------------------------------------------
 *     2013-10-17     larry       v1.0.0        create
 *
 *
 */
package com.china.api.wechat.processor.main;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author:larry
 * @version:v1.0.0
 * @Created:2013-10-17上午10:03:34
 * @Modified:
 */
public class ReplyMapping implements Serializable {

	private static final long serialVersionUID = 1L;
	private List<Mapping> mappingList;

	public String findActionPath(String content) {
		if (mappingList == null || mappingList.size() == 0) {
			return null;
		}
		if (mappingList.size() == 1) {
			return mappingList.get(0).getActionPath();
		}
		if (content.trim().matches("\\d\\d?")) {
			for (Mapping mapping : mappingList) {
				if (mapping.getNumber() == Integer.parseInt(content.trim())) {
					return mapping.getActionPath();
				}
			}
		}else{
			for (Mapping mapping : mappingList) {
				if (mapping.getContent().matches(".*" + content + ".*")) {
					return mapping.getActionPath();
				}
			}
		}
		return null;
	}

	public void addMapping(int number, String content, String path) {
		Mapping mapping = new Mapping(number, content, path);
		if (mappingList == null) {
			mappingList = new ArrayList<Mapping>();
		}
		mappingList.add(mapping);
	}

	public void addMapping(String content, String path) {
		Mapping mapping = new Mapping(content, path);
		if (mappingList == null) {
			mappingList = new ArrayList<Mapping>();
		}
		mappingList.add(mapping);
	}

	public void setSingleMapping(String path) {
		Mapping mapping = new Mapping(path);
		if (mappingList == null) {
			mappingList = new ArrayList<Mapping>();
		}
		mappingList.clear();
		mappingList.add(mapping);
	}

	class Mapping {
		private int number;// 编号
		private String content;// 内容
		private String actionPath;// 处理器路径

		public Mapping() {
		}

		public Mapping(String actionPath) {
			this.actionPath = actionPath;
		}

		public Mapping(String content, String actionPath) {
			this.content = content;
			this.actionPath = actionPath;
		}

		public Mapping(int number, String content, String actionPath) {
			this.number = number;
			this.content = content;
			this.actionPath = actionPath;
		}

		public int getNumber() {
			return number;
		}

		public void setNumber(int number) {
			this.number = number;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public String getActionPath() {
			return actionPath;
		}

		public void setActionPath(String actionPath) {
			this.actionPath = actionPath;
		}

	}

}
